package pl.edu.agh.uddiProxy;

import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import dk.itst.uddi.client.query.FindBindingQuery;
import dk.itst.uddi.client.query.FindBusinessQuery;
import dk.itst.uddi.client.query.FindRelatedBusinessesQuery;
import dk.itst.uddi.client.query.FindServiceQuery;
import dk.itst.uddi.client.types.core.BindingTemplate;
import dk.itst.uddi.client.types.core.BusinessEntity;
import dk.itst.uddi.client.types.core.BusinessService;
import dk.itst.uddi.client.types.core.OperationalInfo;
import pl.edu.agh.uddiProxy.dao.DAOHelper;
import pl.edu.agh.uddiProxy.dao.DynamicParameterDAO;
import pl.edu.agh.uddiProxy.query.FindTModelQuery;
import pl.edu.agh.uddiProxy.types.ServiceDynamicParameters;
import pl.edu.agh.uddiProxy.types.TModel;

public class InquiryClient {
	private dk.itst.uddi.client.InquiryClient inquiryClient;
	private DynamicParameterDAO dynamicParameterDAO = DAOHelper.getDynamicParameterDAO();

	public InquiryClient(dk.itst.uddi.client.InquiryClient inquiryClient) {
		super();
		this.inquiryClient = inquiryClient;
	}
	
	public FindBindingQuery findBinding() {
		return inquiryClient.findBinding();
	}
	
	public FindBusinessQuery findBusiness() {
		return inquiryClient.findBusiness();
	}
	
	public FindRelatedBusinessesQuery findRelatedBusinesses() {
		return inquiryClient.findRelatedBusinesses();
	}
	
	public FindServiceQuery findService() {
		return inquiryClient.findService();
	}
	
	public BindingTemplate getBinding(String key) {
		return inquiryClient.getBinding(key);
	}
	
	public BindingTemplate[] getBindings(String[] keys) {
		return inquiryClient.getBindings(keys);
	}
	
	public BusinessEntity[] getBusinessEntities(String[] keys) {
		return inquiryClient.getBusinessEntities(keys);
	}
	
	public BusinessEntity getBusinessEntity(String businessKey) {
		return inquiryClient.getBusinessEntity(businessKey);
	}
	
	public OperationalInfo getOperationalInfo(String entityKey) {
		return inquiryClient.getOperationalInfo(entityKey);
	}
	
	public OperationalInfo[] getOperationalInfo(String[] entityKeys) {
		return inquiryClient.getOperationalInfo(entityKeys);
	}
	
	public BusinessService getService(String serviceKey) {
		return inquiryClient.getService(serviceKey);
	}
	
	public BusinessService[] getServices(String[] serviceKeys) {
		return inquiryClient.getServices(serviceKeys);
	}
	

	
	public FindTModelQuery findTModel() {
		return new FindTModelQuery(inquiryClient.findTModel());
	}
	
	public TModel getTModel(String tModelKey) {
		TModel model = new TModel(inquiryClient.getTModel(tModelKey));
		ServiceDynamicParameters serviceDynamicParameters = dynamicParameterDAO.getByTModel(tModelKey);
		if (serviceDynamicParameters != null) {
			model.setDynamicParameter(serviceDynamicParameters);
		}
		return model;
	}
	
	public TModel[] getTModels(String[] keys) {
		TModel[] models = new TModel[keys.length];
		for (int i = 0; i < keys.length; i++) {
			models[i] = getTModel(keys[i]);
		}
		
		return models;
	}
	
	public TModel[] getTModels(String[] keys, String query) {
		List<String> list = dynamicParameterDAO.getTModels(query);
		List<TModel> modelList = new LinkedList<TModel>();
		List<String> keyList = Arrays.asList(keys);
		for (int i = 0; i < list.size(); i++) {
			if (keyList.contains(list.get(i))) {
				modelList.add(getTModel(list.get(i)));
			}
		}
		
		TModel[] models = new TModel[modelList.size()];
		return modelList.toArray(models);
	}
	
}
